

import 'package:flutter/material.dart';

import 'HttpConfig/http.dart';

class HttpPageDemo extends StatefulWidget {
  HttpPageDemo({Key key}) : super(key: key);

  @override
  _HttpPageDemoState createState() => _HttpPageDemoState();
}

class _HttpPageDemoState extends State<HttpPageDemo> {

  List listData = new List();

  @override
  void initState() {
    super.initState();
    // ResustHttp.getListData().then((value){
    //   if(value != null){
    //     print(value);
    //     setState(() {
    //       this.listData = value["result"];
    //     });
    //   }
    // });
  }

  Widget _buildFuture(BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        print('还没有开始网络请求');
        return Text('还没有开始网络请求');
      case ConnectionState.active:
        print('active');
        return Text('ConnectionState.active');
      case ConnectionState.waiting:
        print('waiting');
        return Center(
          child: CircularProgressIndicator(),
        );
      case ConnectionState.done:
        print('done');
        if (snapshot.hasError) return Text('Error: ${snapshot.error}');
        return _createListView(context, snapshot);
      default:
        return null;
    }
  }

  Widget _createListView(BuildContext context, AsyncSnapshot snapshot) {

    List movies = snapshot.data['result'];
    return ListView.separated(
      itemBuilder: (context, index) => _itemBuilder(context, index, movies),
      itemCount: movies.length,
      separatorBuilder:(context,index){
          return Divider();
      } ,
    );
  }
  Widget _itemBuilder(BuildContext context, int index, movies) {
   return ListTile(
      title:Text(movies[index]["title"]),
    );
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("HTTP demo"),
      ),
      body:FutureBuilder(
        builder: _buildFuture,
        future:ResustHttp.getListData(),
      )
     // body:LIS
    );
  }
}


